package com.uduew.sjd.ui.repayment

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.net.toUri
import com.uduew.sjd.BR
import com.uduew.sjd.R
import com.uduew.sjd.api.RepaymentLinkApi
import com.uduew.sjd.api.RepaymentMethodApi
import com.uduew.sjd.bean.HttpBean
import com.uduew.sjd.message.PageMessenger
import com.uduew.sjd.net.DataResult
import com.uduew.sjd.request.RepaymentLinkRequester
import com.uduew.sjd.request.RepaymentMethodRequester
import com.uduew.sjd.ui.adapter.RepaymentTypeListAdapter
import com.uduew.sjd.ui.base.BaseFragment
import com.uduew.sjd.ui.base.StateHolder
import com.uduew.sjd.ui.web.WebViewActivity
import com.uduew.sjd.utils.ToastUtils
import com.uduew.sjd.utils.Utils
import com.kongzue.dialogx.dialogs.PopTip
import com.kongzue.dialogx.dialogs.WaitDialog
import com.kunminx.architecture.ui.page.DataBindingConfig
import com.kunminx.architecture.ui.state.State

class ExtensionRepaymentTypeFragment : BaseFragment() {

    lateinit var mState: ExtensionRepaymentTypeSate
    private lateinit var mMessenger: PageMessenger
    private lateinit var mRepaymentMethodRequester: RepaymentMethodRequester
    private lateinit var mRepaymentLinkRequester: RepaymentLinkRequester


    private lateinit var mAdapter: RepaymentTypeListAdapter

    private var orderId: String? = ""
    private var calculator: String? = ""

    override fun initViewModel() {
        mState = getFragmentScopeViewModel(ExtensionRepaymentTypeSate::class.java)
        mMessenger = getApplicationScopeViewModel(PageMessenger::class.java)
        mRepaymentMethodRequester = getFragmentScopeViewModel(RepaymentMethodRequester::class.java)
        mRepaymentLinkRequester = getFragmentScopeViewModel(RepaymentLinkRequester::class.java)

    }

    private fun openUrlInWebView(url: String?) {
        val uri = url?.toUri()
        val intent = Intent(Intent.ACTION_VIEW, uri)
        startActivity(intent)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mRepaymentLinkRequester.result.observe(viewLifecycleOwner) { dataResult: DataResult<HttpBean<RepaymentLinkApi.ResRePaymentLinkBean>> ->
            WaitDialog.dismiss()
            if (!dataResult.responseStatus.isSuccess) {
                ToastUtils.showShortToast(dataResult.responseStatus.responseCode)
                return@observe
            }
            dataResult.result.data.let {
                if (it != null) {
                    openUrlInWebView(it.acidfast)
                }
            }

        }
        mRepaymentMethodRequester.result.observe(viewLifecycleOwner) { dataResult: DataResult<HttpBean<RepaymentMethodApi.ResRePaymentMethodBean>> ->
            WaitDialog.dismiss()
            if (!dataResult.responseStatus.isSuccess) {
                ToastUtils.showShortToast(dataResult.responseStatus.responseCode)
                return@observe
            }
            dataResult.result.data.let {
                if (it != null) {
                    it.methods
                        .take(1)
                        .forEach { item ->
                            item.isSelected = true
                        }
                    mState.typeList.set(it.methods)
                }
            }
        }
        orderId = arguments?.getString("orderId")
        calculator = arguments?.getString("calculator")
        calculator?.let {
            mState.calculator.set(it)
        }
        orderId?.let {
            WaitDialog.show(getString(R.string.loading))
            mRepaymentMethodRequester.request(this, it)
        }
    }

    override fun getDataBindingConfig(): DataBindingConfig? {
        mAdapter = RepaymentTypeListAdapter(requireContext())
        mAdapter.setOnItemClickListener { id, item, position ->
            item.isSelected = !item.isSelected;
            mAdapter.notifyItemChanged(position)
        }

        return DataBindingConfig(
            R.layout.fragment_extension_repayment_type,
            BR.vm,
            mState
        ).addBindingParam(
            BR.click,
            Click()
        ).addBindingParam(BR.adapter, mAdapter)
    }


    inner class Click {
        fun navBack() {
            if (!Utils.checkDebounce()) return
            nav().navigateUp()
        }

        fun nowPay() {
            var selectItems = mAdapter.currentList.filter { it.isSelected }
            if (selectItems.isEmpty()) {
                PopTip.show(getString(R.string.select_repayment_method)).iconWarning()
                return
            }

            WaitDialog.show(getString(R.string.loading))
            var selectItem = selectItems.first()
            val bean = RepaymentLinkApi.ReqRePaymentLinkBean()
            bean.setCindy(orderId)
            bean.setKill(selectItem.kill)
            bean.setBuckthorn(selectItem.buckthorn)
            bean.setZoophytology("disazo")
            mRepaymentLinkRequester.request(this@ExtensionRepaymentTypeFragment, bean)
        }
    }

    class ExtensionRepaymentTypeSate : StateHolder() {
        val typeList: State<ArrayList<RepaymentMethodApi.MethodsBean>> =
            State<ArrayList<RepaymentMethodApi.MethodsBean>>(ArrayList<RepaymentMethodApi.MethodsBean>())
        val calculator: State<String> = State<String>("")

    }

}